/*
 * Game Show
 */
package client;

import both.AbstractFileListener;
import both.TestMessage;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class TestMessageListener extends AbstractFileListener {
    
    private String serverOutputFolder;
    private long lastUpdated = 0;
    
    @Override
    protected void listenerTick() throws Exception {
        File tfile = new File(serverOutputFolder+"/test");
        if(tfile.exists()) {
            long currUpdate = tfile.lastModified();
            if(currUpdate>lastUpdated) {
                TestMessage msg = readMessage(tfile);
                ActionEvent ev;
                if(msg.getIDs().contains(GameShowClient.uniqueID())) {
                    ev = new ActionEvent(this, 0, "test-ok");
                } else {
                    ev = new ActionEvent(this, 0, "test-nonreg");
                }
                sendEvent(ev);
                lastUpdated = currUpdate;
            }
        }
    }
    
    private TestMessage readMessage(File testf) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(TestMessage.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return (TestMessage) jaxbUnmarshaller.unmarshal(testf);
    }

    /**
     * @return the server output folder
     */
    public String getServerOutputFolder() {
        return serverOutputFolder;
    }

    /**
     * @param serverOutputFolder the server output folder to set
     */
    public void setServerOutputFolder(String serverOutputFolder) {
        this.serverOutputFolder = serverOutputFolder;
    }
    
}
